<?php

// 初始化数据库
function mysqlInit($host, $user, $pass, $dbname)
{
    $res = @mysql_connect($host, $user, $pass);
    if (!$res) {
        return false;
    }
    mysql_select_db($dbname);
    mysql_set_charset('utf8');
    return $res;
}
// 加密
function md5Pass($pass)
{
    if (!$pass) {
        return false;
    }
    return md5(md5($pass).'imooc');
}
// 跳转
function toMsgPage($type, $msg=null, $url=null)
{
    $header = "Location:msg.php?type={$type}";
    $header.=$msg?"&msg={$msg}":'';
    $header.=$url?"&url={$url}":'';
    header($header);
    exit;
}
// 上传文件
function uploadFile($file)
{
    if (is_uploaded_file($file['tmp_name'])) {
        $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
        if (in_array($ext, array('jpg','jpeg','gif','png'))) {
            $path = './static/file/';
            $webPath = '/static/file/';
            $fileDir = date('Y/md', $_SERVER['REQUEST_TIME']);
            if (!is_dir($path.$fileDir)) {
                mkdir($path.$fileDir, 0755, true);
            }
            $filename = uniqid().mt_rand(1000, 9999).'.'.$ext;
            $filePath =  $path.$fileDir.'/'.$filename;
            if (!move_uploaded_file($file['tmp_name'], $filePath)) {
                echo "网络原因，请重新上传";
            } else {
                return $webPath.$fileDir.'/'.$filename;
            }
        } else {
            echo "请上传图片格式的文件";
        }
        exit;
    } else {
        echo "请使用post请求进行传输";
    }
}
// 登录验证
function checkLogin()
{
    session_start();
    if (isset($_SESSION['user']) && !empty($_SESSION['user'])) {
        return true;
    } else {
        return false;
    }
}
// 获取当前url
function getUrl()
{
    $url = '';
    $url .= $_SERVER['SERVER_PORT']==443?'https://':'http://';
    $url .= $_SERVER['HTTP_HOST'];
    $url .= $_SERVER['REQUEST_URI'];
    return $url;
}
// 生成当前page
function pageUrl($page, $url = '')
{
    $url = empty($url)?getUrl():$url;
    $pos = strpos($url, '?');
    if ($pos) {
        $queryStr = substr($url, $pos + 1);
        // 整合成一个数组
        parse_str($queryStr, $queryArr);
        if (isset($queryArr['page'])) {
            unset($queryArr['page']);
        }
        $queryArr['page'] = $page;
        $str = http_build_query($queryArr);
        // 拼接成一个新的str
        $prevUrl = substr($url, 0, $pos);
        return $prevUrl.'?'.$str;
    } else {
        return $url .= '?page='.$page;
    }
}
// 分页查询
/**
 * $total 总条数
 * $curentPage 当前页数
 * $pageSize 一页显示的条数
 */
function pages($total, $currentPage, $pageSize, $show=5)
{
    $pageStr = '';
    if ($total>$pageSize) {
        $totalPage = ceil($total/$pageSize);
        $currentPage = $currentPage>$totalPage?$totalPage:$currentPage;
        $from = max(1, $currentPage - intval($show/2));
        $to = $from + $show - 1;
        $pageStr .= '<div class="page-nav">';
        $pageStr .='<ul>';
        if ($currentPage > 1) {
            $prev = $currentPage - 1;
            $pageStr .= "<li><a href=". pageUrl(1) .">首页</a></li>";
            $pageStr .= "<li><a href=".pageUrl($prev).">上一页</a></li>";
        }
        if ($to > $totalPage) {
            $to = $totalPage;
            $from = max(1, $to - $show + 1);
        }
        if ($from > 1) {
            $pageStr .= "<li>...</li>";
        }
        for ($i = $from; $i <= $to ; $i++) {
            if ($i == $currentPage) {
                $pageStr .= "<li><span class='curr-page'>{$currentPage}</span></li>";
            } else {
                $pageStr .= "<li><a href=". pageUrl($i) .">{$i}</a></li>";
            }
        }
        if ($to < $totalPage) {
            $pageStr .= "<li>...</li>";
        }
        if ($currentPage < $totalPage) {
            $next = $currentPage + 1;
            $pageStr .= "<li><a href=".pageUrl($next).">下一页</a></li>";
            $pageStr .= "<li><a href=".pageUrl($totalPage).">尾页</a></li>";
        }
        $pageStr .='</ul>';
        $pageStr .= '</div>';
    }
    return $pageStr;
}
